package sy.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sy.jackson.SysProjectMixin;
import sy.jackson.SysUnitMixin;
import sy.util.MessageObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.model.PerAddress;
import sy.model.SysBuilding;
import sy.model.SysDivision;
import sy.model.SysInformation;
import sy.model.SysProject;
import sy.model.SysProjectInformationMapping;
import sy.model.SysRole;
import sy.model.SysUnit;
import sy.service.SysBuildingServiceI;
import sy.service.SysDivisionServiceI;
import sy.service.SysInformationServiceI;
import sy.service.SysProjectInformationMappingServiceI;
import sy.service.SysProjectServiceI;
import sy.service.SysRoleServiceI;
import sy.service.SysUnitServiceI;
/**
 * @Description: 首页
 * @param 
 * @author     :wjy
 * Create Date : 2017年7月26日 
 */
@Controller
@SessionAttributes("currAccount")
@RequestMapping("infoController")
public class InformationController {
	
	private SysInformationServiceI sysInfoService;
	private SysProjectServiceI sysProjectService;
	private SysProjectInformationMappingServiceI sysProjectInformationMappingService;
	private static final ObjectMapper objectMapper = new ObjectMapper();
	

	public SysInformationServiceI getSysInfoService() {
		return sysInfoService;
	}

	@Autowired
	public void setSysInfoService(SysInformationServiceI sysInfoService) {
		this.sysInfoService = sysInfoService;
	}

	public SysProjectServiceI getSysProjectService() {
		return sysProjectService;
	}

	@Autowired
	public void setSysProjectService(SysProjectServiceI sysProjectService) {
		this.sysProjectService = sysProjectService;
	}
	

	public SysProjectInformationMappingServiceI getSysProjectInformationMappingService() {
		return sysProjectInformationMappingService;
	}

	@Autowired
	public void setSysProjectInformationMappingService(
			SysProjectInformationMappingServiceI sysProjectInformationMappingService) {
		this.sysProjectInformationMappingService = sysProjectInformationMappingService;
	}

	/************************** view controller ***************************/



	/**
	 * @Description: 首页视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		
		
		List<SysInformation> infoList = sysInfoService.list();
		
		model.addAttribute("infoList",infoList);
		return "baseInfo/information/information";
	}
	
	/**
	 * @Description: 新增视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		String uuid = UUID.randomUUID().toString();
		SysInformation infomation = new SysInformation();
		infomation.setId(uuid);
		List<SysProject> projectList = sysProjectService.list();
		
		model.addAttribute("infomation",infomation);
		model.addAttribute("projectList", projectList);
		model.addAttribute("type",1);
		
		return "baseInfo/information/informationFrom";
	}

	/**
	 * @Description: 编辑视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam String id,Model model){
		
		SysInformation infomation  = sysInfoService.getSysInformationById(id);
		List<SysProject> projectList = sysProjectService.list();
		List<SysProject> projectListCheck = new ArrayList<SysProject>();
		
		/**
		 * 通过information实体返回中间表，通过中间表构造project实体
		 */
		List<SysProjectInformationMapping> list = sysProjectInformationMappingService.listSysProjectInformationMappingByKey(infomation.getId());
		for(SysProjectInformationMapping obj : list){
			projectListCheck.add(obj.getSysProject());
		}
		model.addAttribute("infomation",infomation);
		model.addAttribute("projectList", projectList);
		model.addAttribute("projectListCheck", projectListCheck);
		model.addAttribute("type",2);
		
		return "baseInfo/information/informationFrom";
	}

	/************************** data controller ***************************/
	
	/**
	 * @Description: 增加
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年8月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/new/{projectIds}", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	private MessageObject newObjectInfo( @RequestBody SysInformation information, Model model, @PathVariable String[] projectIds) {
		
		MessageObject messageObject = new MessageObject();
		try {
			sysInfoService.save(information,projectIds);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 删除
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String[] ids) {
		MessageObject messageObject = new MessageObject();
		try {
			sysInfoService.remove(ids);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	/**
	 * 
	 * @Title: editObjectInfo
	 * @Description: TODO
	 * @param: @param information
	 * @param: @param model
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月1日 上午8:51:06
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/edit/{projectIds}", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody SysInformation information, Model model, @PathVariable ("projectIds") String[] projectIds) {
		MessageObject messageObject = new MessageObject();
		try {
			sysInfoService.update(information,projectIds);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 返回全部json数据
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {

		List<SysInformation> informationList = sysInfoService.list();
		try {
			return objectMapper.writeValueAsString(informationList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 	
	 * @param id
	 * @author     :wjy
	 * @Create Date : 2017年8月26日 
	 */
	@RequestMapping(value="/view/getProjectsByInfoId/{id}",method=RequestMethod.GET)
	public String getProjectsByInfoId(@PathVariable String id,Model model){
		/**
		 * 通过公共信息id获取项目信息中间表符合条件的项目，得到该公告信息所属的所有项目
		 */
		List<SysProjectInformationMapping> projInfoList = sysProjectInformationMappingService.listSysProjectInformationMappingByKey(id);
		List<SysProject> infoProjects = new ArrayList<SysProject>();
		
		for(SysProjectInformationMapping projInfo :projInfoList) {
			String proId = projInfo.getSysProject().getId();
			SysProject project =sysProjectService.getSysProjectById(proId) ;
			infoProjects.add(project);
		}
		
		model.addAttribute("infoProjects",infoProjects);
		return "baseInfo/information/informationProjects";
	}
	
	
}
